package com.whiteape.book.ui.fragment;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.github.iielse.switchbutton.SwitchView;
import com.whiteape.book.R;
import com.whiteape.book.core.manager.ReadSettingManager;
import com.whiteape.book.core.page.PageLoader;
import com.whiteape.book.ui.activity.BookReadActivity;
import com.whiteape.book.ui.base.BaseFragment;

import butterknife.BindView;

public class ReadSettingAdvancedFragment extends BaseFragment implements SwitchView.OnStateChangedListener {
    @BindView(R.id.sb_hide_status_bar)
    SwitchView svHideStatusBar;

    @BindView(R.id.sb_hide_nav_bar)
    SwitchView svHideNavBar;

    @BindView(R.id.sb_click_to_change_page)
    SwitchView svClickToChangePage;

    @BindView(R.id.sb_volume_key_to_change_page)
    SwitchView svVolumeKeyToChangePage;

    @BindView(R.id.sb_show_time)
    SwitchView svShowTime;

    @BindView(R.id.sb_show_battery)
    SwitchView svShowBattery;

    @BindView(R.id.sb_show_chapter_title)
    SwitchView svShowChapterTitle;

    @BindView(R.id.sb_show_page_progress)
    SwitchView svShowPageProgress;

    @BindView(R.id.sb_show_catalog_progress)
    SwitchView svShowCatalogProgress;

    @BindView(R.id.sb_keep_screen_on)
    SwitchView svKeepScreenOn;

    private boolean hideStatusBar;
    private boolean hideNavBar;
    private boolean fullScreenClickToChangePage;
    private boolean volumeToChangePage;
    private boolean showTime;
    private boolean showBattery;
    private boolean showChapterTitle;
    private boolean showPageProgress;
    private boolean showCatalogProgress;
    private boolean keepScreenOn;

    private ReadSettingManager mReadSettingManager;
    private Activity mActivity;
    private PageLoader mPageLoader;

    public ReadSettingAdvancedFragment(/*Activity activity, PageLoader pageLoader*/) {
//        this.mActivity = activity;
//        this.mPageLoader = pageLoader;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.mActivity = getActivity();
        if(mActivity != null) {
            this.mPageLoader = ((BookReadActivity) mActivity).getPageLoader();
        }
    }

    @Override
    protected int getContentId() {
        return R.layout.dialog_read_setting_advance;
    }


    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mReadSettingManager = ReadSettingManager.getInstance();

        hideStatusBar = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_STATUS_BAR);
        hideNavBar = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_NAV_BAR);
        fullScreenClickToChangePage = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_FULLSCREEN_CLICK_TO_CHANGE_PAGE);
        volumeToChangePage = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_VOLUME_TO_CHANGE_PAGE);
        showTime = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_TIME);
        showBattery = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_BATTERY);
        showChapterTitle = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CHAPTER_TITLE);
        showPageProgress = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_PAGE_PROGRESS);
        showCatalogProgress = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CATALOG_PROGRESS);
        keepScreenOn = mReadSettingManager.getReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_KEEP_SCREEN_ON);

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        svHideStatusBar.setOpened(hideStatusBar);
        svHideNavBar.setOpened(hideNavBar);
        svClickToChangePage.setOpened(fullScreenClickToChangePage);
        svVolumeKeyToChangePage.setOpened(volumeToChangePage);
        svShowTime.setOpened(showTime);
        svShowBattery.setOpened(showBattery);
        svShowChapterTitle.setOpened(showChapterTitle);
        svShowPageProgress.setOpened(showPageProgress);
        svShowCatalogProgress.setOpened(showCatalogProgress);
        svKeepScreenOn.setOpened(keepScreenOn);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        svHideStatusBar.setOnStateChangedListener(this);
        svHideNavBar.setOnStateChangedListener(this);
        svClickToChangePage.setOnStateChangedListener(this);
        svVolumeKeyToChangePage.setOnStateChangedListener(this);
        svShowTime.setOnStateChangedListener(this);
        svShowBattery.setOnStateChangedListener(this);
        svShowChapterTitle.setOnStateChangedListener(this);
        svShowPageProgress.setOnStateChangedListener(this);
        svShowCatalogProgress.setOnStateChangedListener(this);
        svKeepScreenOn.setOnStateChangedListener(this);
    }

    @Override
    public void toggleToOn(SwitchView view) {
        //TODO 坑，这里还需要手动toggleSwitch!
        boolean barChanged = false;
        view.toggleSwitch(true);
        if (view == svHideStatusBar) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_STATUS_BAR, true);
            barChanged = true;
//            mActivity.recreate();
        } else if (view == svHideNavBar) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_NAV_BAR, true);
            barChanged = true;
//            mActivity.recreate();
        } else if (view == svClickToChangePage) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_FULLSCREEN_CLICK_TO_CHANGE_PAGE, true);
        } else if (view == svVolumeKeyToChangePage) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_VOLUME_TO_CHANGE_PAGE, true);
        } else if (view == svShowTime) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_TIME, true);

        } else if (view == svShowBattery) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_BATTERY, true);

        } else if (view == svShowChapterTitle) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CHAPTER_TITLE, true);

        } else if (view == svShowPageProgress) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_PAGE_PROGRESS, true);
        } else if (view == svKeepScreenOn) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_KEEP_SCREEN_ON, true);
        } else if (view == svShowCatalogProgress) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CATALOG_PROGRESS, true);
        }
        updateParams(barChanged);

    }

    @Override
    public void toggleToOff(SwitchView view) {
        //TODO 坑，这里还需要手动toggleSwitch!
        view.toggleSwitch(false);
        boolean barChanged = false;
        if (view == svHideStatusBar) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_STATUS_BAR, false);
            barChanged = true;
//            mActivity.recreate();
        } else if (view == svHideNavBar) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_HIDE_NAV_BAR, false);
            barChanged = true;
//            mActivity.recreate();
        } else if (view == svClickToChangePage) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_FULLSCREEN_CLICK_TO_CHANGE_PAGE, false);
        } else if (view == svVolumeKeyToChangePage) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_VOLUME_TO_CHANGE_PAGE, false);
        } else if (view == svShowTime) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_TIME, false);

        } else if (view == svShowBattery) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_BATTERY, false);

        } else if (view == svShowChapterTitle) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CHAPTER_TITLE, false);

        } else if (view == svShowPageProgress) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_PAGE_PROGRESS, false);
        } else if (view == svKeepScreenOn) {
            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_KEEP_SCREEN_ON, false);
        } else if (view == svShowCatalogProgress) {

            mReadSettingManager.setReadOtherSettings(ReadSettingManager.READ_OTHER_SETTING_SHOW_CATALOG_PROGRESS, false);
        }
        updateParams(barChanged);
    }


    private void updateParams(boolean barChanged) {
        if(mActivity == null){
            return;
        }
        ((BookReadActivity)mActivity).initBar();
        if (mPageLoader != null) {
            mPageLoader.updateOtherSettings(barChanged);
        }

    }
}
